<template>
  <div class="search-user-container" v-if="callStatus !== 'connected'">
    <h3>请选择面试者</h3>
    <el-select v-model="mvalue" placeholder="请选择" style="width:400px; margin: 10px auto;" @change="select">
      <el-option
        v-for="item in options"
        :key="item.value"
        :label="item.label"
        :value="item.value">
      </el-option>
    </el-select>
    <!-- 显示待面试者列表 -->
    <div class="test-scroller">
      <div v-for="i in resumeOnelist" :key="i.id" class="user-info" @click="getId(i.id,i.uid)">
        {{i.uname}}<br>
        {{i.job}}
      </div>
    </div>
    <div class="search-section">
      <el-input
        class="inline-input"
        v-model="searchInput"
        maxlength="11"
        placeholder="请输入用户ID"
      ></el-input>
    </div>
    <div v-show="callStatus !== 'connected'" class="search-user-list">
      <div
        v-if="callStatus === 'calling' && isInviter"
        class="calling-user-footer"
      >
        <el-button class="user-item-join-btn calling">呼叫中...</el-button>
        <el-button
          class="user-item-cancel-join-btn"
          :disabled="cancel"
          :loading="cancel"
          @click="handleCancelCallBtnClick"
          >取消</el-button
        >
      </div>
      <el-button
        v-else
        @click="handleCallBtnClick(searchInput)"
        :disabled="call"
        class="user-item-join-btn"
        >呼叫</el-button
      >
    </div>
  </div>
</template>

<script>
import { mapState } from "vuex";
import { getSearchHistory } from "../../utils";

export default {
  name: "SearchUser",
  props: {
    callFlag: {
      type: Boolean,
    },
    cancelFlag: {
      type: Boolean,
    },
  },
  data() {
    return {
      searchInput: null,
      callUserId: "",
      searchResultList: [],
      searchHistoryUser: getSearchHistory(),
      call: false,
      cancel: false,
      resumeOnelist: [],
      options: [{value: '3',label: '一面'}, 
                {value: '6',label: '二面'}, 
                {value: '8',label: '三面'}],
      mvalue: '一面'
    };
  },
  computed: {
    ...mapState({
      loginUserInfo: (state) => state.loginUserInfo,
      meetingUserIdList: (state) => state.meetingUserIdList,
      callStatus: (state) => state.callStatus,
      isAccepted: (state) => state.isAccepted,
      isInviter: (state) => state.isInviter,
    }),
    userList: function () {
      if (this.searchInput === "" && this.searchHistoryUser.length !== 0) {
        return this.searchHistoryUser;
      }
      return this.searchResultList;
    },
  },
  watch: {
    callStatus: function (newStatus, oldStatus) {
      if (newStatus !== oldStatus && newStatus === "connected") {
        this.searchInput = "";
        this.searchResultList = [];
      }
      if (newStatus === "idle") {
        this.callUserId = "";
      }
    },
    callFlag(newVal) {
      this.call = newVal;
    },
    cancelFlag(newVal) {
      this.cancel = newVal;
    },
  },
  methods: {
    getId: function (reid,uid){
      console.log("当前面试id：",reid);
      console.log("接受面试者：",uid);
      this.getUserInfo(uid);
      this.$store.commit("updateReId", reid);
    },

    handleCallBtnClick: function (param) {
      if(param == null || param === ""){
        this.$message("请输入正确用户ID");
        return;
      }
      if (param === this.loginUserInfo.userId) {
        this.$message("请输入正确用户ID");
        return;
      }
      this.call = true;
      this.callUserId = param;
      this.$emit("callUser", { param });
    },
    handleCancelCallBtnClick: function () {
      // 对方刚接受邀请，但进房未成功
      this.cancel = true;
      this.$emit("cancelCallUser");
    },
    //查找投递记录为待面试
    selectResume: function (state) {
      console.log("状态：", state);
      var id = this.$store.state.userId;
      console.log("获取userId:", id);
      var url = this.$store.state.ip + "/userRe/userReC3";
      // 参数
      let param = new URLSearchParams();
      param.append("cid", id);
      param.append("state", state);
      this.$axios({
        method: "post",
        url: url,
        data: param,
      }).then((res) => {
        console.log(res.data);
        this.resumeOnelist = res.data;
      });
    },
    // 获取用户信息
    getUserInfo:function(uid) {
      var url = this.$store.state.ip + "/user/getUserInfo";
      // 参数
      let param = new URLSearchParams();
      param.append("id", uid);
      this.$axios({
        method: "post",
        url: url,
        data: param,
      }).then((res) => {
        console.log(res.data);
        this.searchInput = res.data.iphone;
      });
    },
    select:function(){
      console.log("面试代号：",this.mvalue);
      this.$store.commit("updateTimes", this.mvalue);
      this.selectResume(this.mvalue);
    },
  },
  beforeMount() {
    this.selectResume(3);
  },
};
</script>

<style scoped>
.user-info{
  height: 50px;
  width: 345px;
  margin: 5px 15px;
  border: 1px solid #2c3e50;
  text-align: center;
  border-radius: 8px;
}
.test-scroller {
  border: 1px solid #ccc;
  height: 300px !important;
  -webkit-overflow-scrolling: touch;
  /* overflow-scrolling: touch; */
  background: #fff;
  color: #2c3e50;
  height: 30rem;
  overflow: scroll;
}
.search-user-container {
  width: 400px;
  margin: 10px auto 0;
}
.search-section {
  margin: 10px auto;
  display: flex;
  flex-direction: row;
}
.search-user-btn {
  margin-left: 10px;
}
.search-user-list {
  padding-top: 20px;
}
.search-user-list-title {
  margin-top: 20px;
  font-size: 18px;
  text-align: left;
}
.user-item {
  display: flex;
  flex-direction: row;
  align-items: center;
  justify-content: space-between;
  margin-top: 10px;
}
.user-item-info {
  display: flex;
  flex-direction: row;
  align-items: center;
}
.user-item-avatar-wrapper img {
  width: 50px;
  height: 50px;
  border-radius: 50%;
}
.user-item-username {
  margin-left: 20px;
}
@media screen and (max-width: 767px) {
  .search-user-container {
    width: 90%;
  }
}
</style>
